package com.yanqu.road.dao.impl.zhaoshang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.zhaoshang.ZsProjectNotice;
import com.yanqu.road.entity.zhaoshang.ZsProjectNoticeParam;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZsProjectNoticeDaoImpl extends TempDataObjectDao<ZsProjectNotice> {

    @Override
    protected boolean addData(ZsProjectNotice data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_zs_project_notice(projectId, noticeType, addTime, paramList) values (?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getProjectId());
            call.setInt(i++, data.getNoticeType());
            call.setInt(i++, data.getAddTime());
            call.setString(i++, JSON.toJSONString(data.getParamList()));
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(ZsProjectNotice data) {
        return false;
    }

    @Override
    protected ZsProjectNotice parseData(ResultSet rs) throws SQLException {
        ZsProjectNotice data = new ZsProjectNotice();
        data.setAddTime(rs.getInt("addTime"));
        data.setNoticeType(rs.getInt("noticeType"));
        data.setProjectId(rs.getLong("projectId"));
        String paramList = rs.getString("paramList");
        if(!StringUtils.isNullOrEmpty(paramList)){
            data.setParamList(JSON.parseObject(paramList, new TypeReference<List<ZsProjectNoticeParam>>() {}));
        }
        return data;
    }

    public Map<Long, List<ZsProjectNotice>> getZsAllProjectNoticeMap(int beginTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<ZsProjectNotice>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_c_zs_project_notice where projectId > (select projectId from t_c_zs_project where openTime < ? order by projectId desc LIMIT 1)  order by addTime asc";
        try {
            int i=1;
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(i++, beginTime);
            rs = call.executeQuery();
            while (rs.next()) {
                ZsProjectNotice data = parse(rs);
                if(!dataMap.containsKey(data.getProjectId())){
                    dataMap.put(data.getProjectId(), new ArrayList<>());
                }
                dataMap.get(data.getProjectId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
